﻿using System;
using System.Windows.Forms;

namespace DomainNameServer
{

    public partial class AcceptableForm : Form
    {
        AcceptFormOption acceptOption = new AcceptFormOption();
        public IAcceptableFormBody form;
        public AcceptableForm(Form form, AcceptFormOption option)
        {
            InitializeComponent();
            this.acceptOption = option;
            this.form = (IAcceptableFormBody)form;
            this.Text = form.Text;
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;
            this.panelAcceptableContainer.Controls.Add(form);
            form.Dock = DockStyle.Fill;
            this.StartPosition = FormStartPosition.CenterParent;
            this.panelAcceptableContainer.Width = form.Width;
            this.panelAcceptableContainer.Height = form.Height;
            form.Visible = true;
            form.AcceptButton = this.buttonOk; ;
            form.CancelButton = this.buttonCancel;
            this.CenterToParent();
            (form as Form).Focus();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            form.Cancel();
            this.DialogResult = (form as Form).DialogResult;
            this.Close();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            form.Accept(acceptOption);
            this.DialogResult = (form as Form).DialogResult;
            if (!acceptOption.KeepWindow)
                this.Close();
        }
    }
    public static class AcceptableFormHelper
    {
        public static AcceptableForm CreateInstance(Form form, AcceptFormOption option)
        {
            return new AcceptableForm(form, option);
        }
        public static DialogResult ShowDialogAsAcceptable(this Form form, AcceptFormOption option)
        {
            var acceptableFormBody = form as IAcceptableFormBody;
            if (acceptableFormBody == null)
                throw new Exception("the form should inherited from IAcceptableFormBody for calling ShowDialogAsAcceptable");
            return new AcceptableForm(form, option).ShowDialog();
        }

        public static DialogResult ShowDialogAsAcceptable(this Form form, IWin32Window owner, AcceptFormOption option)
        {
            var acceptableFormBody = form as IAcceptableFormBody;
            if (acceptableFormBody == null)
                throw new Exception("the form should inherited from IAcceptableFormBody for calling ShowDialogAsAcceptable");
            return new AcceptableForm(form, option).ShowDialog(owner);
        }

        public static DialogResult ShowDialogAsAcceptable(this Form form, IWin32Window owner)
        {
            var acceptableFormBody = form as IAcceptableFormBody;
            if (acceptableFormBody == null)
                throw new Exception("the form should inherited from IAcceptableFormBody for calling ShowDialogAsAcceptable");
            return new AcceptableForm(form, new AcceptFormOption()).ShowDialog(owner);
        }
    }

    public interface IAcceptableFormBody
    {
        void Accept(AcceptFormOption option);
        void Cancel();
    }
}
